_package("alz.mui");

_import("alz.mui.ActionElement");

/**
 * form元素的封装
 */
_class("FormElement", ActionElement, function(){
	this._init = function(){
		_super._init.call(this);
		this._elements = [];
		this._submitButton = null;
	};
	this.bind = function(obj, conf){  //{parent,id}
		//this.setParent(conf.parent);
		//this.setParent(obj.parentNode);
		this.init(obj);
	};
	this.init = function(obj){
		_super.init.apply(this, arguments);
		var _this = this;
		/*
		this._self.onmouseup = function(){
			_this._submitButton.setState("normal");
		};
		*/
		this._self.onsubmit = function(ev){
			ev = ev || window.event;
			_this._submitButton.setState("normal");
			return _this.dispatchAction(this, ev);
		};
		this.initFormButtons(this._self);
		//this.initCustomElements(this._self);
	};
	this.dispose = function(){
		this._submitButton = null;
		for(var i = 0, len = this._elements.length; i < len; i++){
			this._elements[i].dispose();
			this._elements[i] = null;
		}
		this._elements = [];
		this._self.onsubmit = null;
		_super.dispose.apply(this);
	};
	/**
	 * 处理FORM内的所有submit按钮
	 */
	this.initFormButtons = function(frm){
		var elements = frm.elements;
		for(var i = 0, len = elements.length; i < len; i++){
			var el = elements[i];
			if(el.tagName == "INPUT" && el.type == "submit" && !el._init){
				el._init = true;
				var btn = new LinkButton();
				btn.bind(el);
				//btn.onclick = onAction;
				btn.setActionManager(this._actionManager);
				this._actionManager.add(el);
				this._submitButton = btn;
				btn = null;
			}
		}
	};
	/* *
	 * 初始化自定义的表单元素
	 */
	/*
	this.initCustomElements = function(frm){
		//提前收集表单元素，防止后面处理自定义元素时，可能往表单中添加新元素而引起问题
		var elements = [];
		var nodes = frm.elements;
		for(var i = 0, len = nodes.length; i < len; i++){
			elements.push(nodes[i]);
		}
		nodes = null;
		for(var i = 0, len = elements.length; i < len; i++){
			var el = elements[i];
			switch(el.tagName){
			case "INPUT":
				switch(el.type){
				case "text":
				case "hidden":
					var className = el.getAttribute("_ui");
					if(className){  //只处理有_ui属性的元素
						if(el.type == "text"){
							el.style.display = "none";
						}
						var clazz = __context__.__classes__[className];
						var component = new clazz();
						component.bindElement(el, this.getApp());
						this._elements.push(component);
						component = null;
					}
					break;
				}
				break;
			}
			el = null;
		}
		elements = null;
	};
	*/
});